<template>
  <a-table :columns="columns" :data-source="data" :pagination="pagination" @change="doTableChange">
    <template #headerCell="{ column }">
      <template v-if="column.key === 'name'">
        <span>
          <smile-outlined />
          Name
        </span>
      </template>
    </template>

    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'name'">
        <a>
          {{ record.name }}
        </a>
      </template>
      <template v-else-if="column.key === 'tags'">
        <span>
          <a-tag
            v-for="tag in record.tags"
            :key="tag"
            :color="tag === 'loser' ? 'volcano' : tag.length > 5 ? 'geekblue' : 'green'"
          >
            {{ tag.toUpperCase() }}
          </a-tag>
        </span>
      </template>
      <template v-else-if="column.key === 'action'">
        <span>
          <a>Invite 一 {{ record.name }}</a>
          <a-divider type="vertical" />
          <a>Delete</a>
          <a-divider type="vertical" />
          <a class="ant-dropdown-link">
            More actions
            <down-outlined />
          </a>
        </span>
      </template>
    </template>
  </a-table>
</template>
<script lang="ts" setup>
import { SmileOutlined, DownOutlined } from '@ant-design/icons-vue'
import { computed, onMounted, reactive, ref } from 'vue'
import type { User } from '@/proto/user.ts'
import { userList } from '@/request/api.ts'
import { PageInfo, UserListReq } from '@/proto/user.ts'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '用户昵称',
    dataIndex: 'name',
  },
  {
    title: '用户地址',
    dataIndex: 'address',
  },
  {
    title: '用户账户',
    key: 'account',
  },
  {
    title: '用户状态',
    dataIndex: 'desc',
  },
]

const data = ref<User[]>([])
const current = ref(1)
const total = ref(0)

const searchPage = reactive<PageInfo>(
  PageInfo.fromObject({
    page_number: 1,
    page_size: 10,
  }),
)

const fetchData = async () => {
  const res = await userList(
    UserListReq.fromObject({
      keyword: '',
      page_info: searchPage,
    }),
  )
  data.value = res.user_list
  current.value = res.page_info.page_number
  total.value = res.page_info.total_page
}

onMounted(() => {
  fetchData()
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchPage.page_number ?? 1,
    pageSize: searchPage.page_size ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total) => `共 ${total} 条`,
  }
})

// 表格变化处理
const doTableChange = (page: any) => {
  searchPage.page_number = page.current
  searchPage.page_size = page.pageSize
  fetchData()
}
</script>
